{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "04223.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/useful_program/blob/master/DL/PyTorch/04223.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2ZIYVDWB_oar",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "F5B8f0PVJNVI",
        "colab_type": "code",
        "outputId": "2f13f8bd-2a85-4c66-8ed0-41743ab6614f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 689
        }
      },
      "source": [
        "y_hat = np.random.rand(10, 20)\n",
        "y_hat"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[0.10580819, 0.86981642, 0.51542053, 0.5167226 , 0.50571762,\n",
              "        0.73362106, 0.54077828, 0.04983206, 0.54742774, 0.76406249,\n",
              "        0.09593559, 0.8299272 , 0.28273733, 0.13489463, 0.57709925,\n",
              "        0.91959599, 0.90615403, 0.0084293 , 0.18151055, 0.1440634 ],\n",
              "       [0.8284783 , 0.45206863, 0.46050088, 0.71157989, 0.25015407,\n",
              "        0.03483063, 0.32365724, 0.70421059, 0.08615329, 0.19322097,\n",
              "        0.17081345, 0.09919925, 0.77383494, 0.90852872, 0.95756414,\n",
              "        0.41004983, 0.58018579, 0.35297444, 0.61599793, 0.86646915],\n",
              "       [0.79674929, 0.17184742, 0.89720093, 0.29715881, 0.15723422,\n",
              "        0.47147416, 0.36285434, 0.15000075, 0.63671909, 0.74183347,\n",
              "        0.42097581, 0.82003731, 0.38709602, 0.31454293, 0.37566127,\n",
              "        0.10666487, 0.08784222, 0.70503733, 0.86553082, 0.08185703],\n",
              "       [0.03865038, 0.13272524, 0.35447624, 0.39350588, 0.80352911,\n",
              "        0.53967823, 0.16349988, 0.29482673, 0.50515337, 0.46301416,\n",
              "        0.89044602, 0.45373542, 0.57229191, 0.19133859, 0.30376467,\n",
              "        0.19113311, 0.88104041, 0.61612562, 0.85208455, 0.19834865],\n",
              "       [0.08379099, 0.29837455, 0.69273668, 0.27563807, 0.71267567,\n",
              "        0.26077346, 0.40710794, 0.70888662, 0.0077659 , 0.54226347,\n",
              "        0.90161125, 0.89769332, 0.60351454, 0.05703944, 0.92976995,\n",
              "        0.72452054, 0.44184009, 0.38199585, 0.83133351, 0.953507  ],\n",
              "       [0.05252278, 0.11148781, 0.94605624, 0.72139715, 0.12073301,\n",
              "        0.42412167, 0.84425047, 0.6501746 , 0.66570667, 0.25664674,\n",
              "        0.57754674, 0.05022326, 0.46485842, 0.66689554, 0.99691157,\n",
              "        0.21992928, 0.26224266, 0.26321737, 0.86346112, 0.18803387],\n",
              "       [0.55853555, 0.21314322, 0.36643377, 0.74662698, 0.7746254 ,\n",
              "        0.42307257, 0.9126871 , 0.90098603, 0.73258205, 0.39219496,\n",
              "        0.5135921 , 0.11625785, 0.14461102, 0.59967578, 0.63912899,\n",
              "        0.77980019, 0.54633074, 0.80575387, 0.53123875, 0.04404621],\n",
              "       [0.51533819, 0.43841788, 0.2992712 , 0.95223364, 0.19091249,\n",
              "        0.01317484, 0.6787262 , 0.45624161, 0.60173929, 0.13969522,\n",
              "        0.54987604, 0.36419555, 0.08176366, 0.80951056, 0.52488054,\n",
              "        0.22459702, 0.22830913, 0.47224701, 0.73609387, 0.52189213],\n",
              "       [0.13077909, 0.35412657, 0.36293739, 0.71345685, 0.27932528,\n",
              "        0.03830109, 0.92429868, 0.98969502, 0.40201505, 0.27503899,\n",
              "        0.33643975, 0.22238367, 0.74394687, 0.98606425, 0.51101898,\n",
              "        0.98296945, 0.1634309 , 0.35993081, 0.07646821, 0.40197063],\n",
              "       [0.60390681, 0.09572523, 0.37143289, 0.49422582, 0.46352783,\n",
              "        0.76240769, 0.8239752 , 0.73951739, 0.29662601, 0.23381983,\n",
              "        0.88305015, 0.67208438, 0.99393854, 0.19309184, 0.24245408,\n",
              "        0.51624465, 0.78559965, 0.31374585, 0.47163232, 0.4042731 ]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "a55gaigrAcQI",
        "colab_type": "code",
        "outputId": "e8b15823-89b3-449c-c1c2-2f7602515ad1",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "y = np.arange(10)\n",
        "y"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fNTjyE16JCoy",
        "colab_type": "text"
      },
      "source": [
        "## PyTorch"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "E7l4DHUoA3fF",
        "colab_type": "code",
        "outputId": "2939f022-c854-446c-a7b0-8ed1e83e5b34",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "import torch\n",
        "from torch import nn \n",
        "\n",
        "y_hat_t = torch.tensor(y_hat)\n",
        "y_t = torch.tensor(y)\n",
        "\n",
        "loss = nn.CrossEntropyLoss()\n",
        "l = loss(y_hat_t, y_t)\n",
        "l"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(3.0155, dtype=torch.float64)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "G0T09j6qFLp9",
        "colab_type": "text"
      },
      "source": [
        "## MXNet"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mWB1Fy-UXyJW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# !pip install mxnet "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qUgnYEkFD4ca",
        "colab_type": "code",
        "outputId": "0a6ba7f3-14e7-4db8-b662-d720c700ae7a",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        }
      },
      "source": [
        "from mxnet import nd \n",
        "from mxnet.gluon import loss as gloss \n",
        "\n",
        "y_hat_m = nd.array(y_hat)\n",
        "y_m = nd.array(y)\n",
        "\n",
        "loss = gloss.SoftmaxCrossEntropyLoss()\n",
        "l = loss(y_hat_m, y_m)\n",
        "l"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[3.3975046 3.074347  2.5793726 3.0782323 2.8615007 3.0859313 2.6513987\n",
              " 3.0092652 3.106083  3.3111794]\n",
              "<NDArray 10 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bUYPydUNbXsc",
        "colab_type": "text"
      },
      "source": [
        "CrossEntropy： PyTorch 是一个 batch 返回一个 loss，是取过平均数的。MXNet 是一个样本一个 loss，没有取平均数。如果要一样，PyTorch 的 l 要乘以 batch_size，也就是乘以 256."
      ]
    }
  ]
}